﻿using IMES.Common.Domain.GoldIngotModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;

namespace GoldIngot.API.Application.Commands.SigningWorkCommand
{
    public class SigningWorkDeleteHandler : IRequestHandler<SigningWorkDeleteCommand, Result>
    {
        private readonly IBaseRepositorys<SigningWork> signingRep;
        private readonly ILogger<SigningWorkDeleteHandler> logger;

        public SigningWorkDeleteHandler(IBaseRepositorys<SigningWork> signingRep, ILogger<SigningWorkDeleteHandler> logger)
        {
            this.signingRep = signingRep;
            this.logger = logger;
        }

        public async Task<Result> Handle(SigningWorkDeleteCommand request, CancellationToken cancellationToken)
        {
            var quest = new Result() { Code = (int)CodeEnum.success };

            string[] ids=request.Ids.Split(',');
            foreach (var id in ids)
            {
                var res = signingRep.GetAll().FirstOrDefault(x => x.SigningId == Convert.ToInt32(id));
                if (res == null)
                {
                    continue;
                }
                res.SigningWorkDeleted = true;
                quest.Code += signingRep.Update(res);
            }
            return quest;
        }
    }
}
